package com.wish.g1.finance.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

//import com.wish.g1.business.entity .Tenant;
//import com.wish.g1.business.service.ITenantService;
import com.wish.g1.finance.entity.Product;
import com.wish.g1.finance.entity.RentInstallment;
import com.wish.g1.finance.entity.query.RentInstallmentQuery;
import com.wish.g1.finance.entity.vo.RentInstallmentVo;
import com.wish.g1.finance.mapper.RentInstallmentMapper;
import com.wish.g1.finance.service.IProductService;
import com.wish.g1.finance.service.IRentInstallmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 房租分期申请 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2022-11-18
 */
@Service
public class RentInstallmentServiceImpl extends ServiceImpl<RentInstallmentMapper, RentInstallment> implements IRentInstallmentService {

    @Autowired
    IProductService productService;

    @Autowired
    RentInstallmentMapper rentInstallmentMapper;

//    @Autowired(required = false)
//    ITenantService tenantService;

    @Override
    public List<RentInstallmentVo> listRent(RentInstallmentQuery rentInstallmentQuery) {
        RentInstallment rentInstallmentQ = new RentInstallment();
        BeanUtils.copyProperties(rentInstallmentQuery,rentInstallmentQ);

        QueryWrapper<RentInstallment> rentInstallmentQueryWrapper = new QueryWrapper<>();
        rentInstallmentQueryWrapper.setEntity(rentInstallmentQ);
        List<RentInstallment> installmentList = this.list(rentInstallmentQueryWrapper);

        List<Long> productIds = installmentList.stream().map(rentInstallment -> rentInstallment.getJrProductId())
                .collect(Collectors.toList());
        List<String> bussinessId = installmentList.stream().map(rentInstallment -> rentInstallment.getBussinessId()).collect(Collectors.toList());
        List<Product> productList = productService.listByIds(productIds);
//        System.err.println(bussinessId);
//        List<Tenant> tenantList = tenantService.listByIds(bussinessId);

        Map<Long, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, product -> product));
//        Map<Integer, Tenant> tenantMap = tenantList.stream().collect(Collectors.toMap(Tenant::getId, tenant -> tenant));


        Map<Long, RentInstallment> rentInstallmentMap = installmentList.stream().collect(Collectors.toMap(RentInstallment::getId, rentInstallment -> rentInstallment));

        ArrayList<RentInstallmentVo> rentInstallmentVoArrayList = new ArrayList<>();

        for (RentInstallment rentInstallment : installmentList) {
            System.err.println(rentInstallment);
            RentInstallmentVo rentInstallmentVo = new RentInstallmentVo();
            BeanUtils.copyProperties(rentInstallment,rentInstallmentVo);

            rentInstallmentVoArrayList.add(rentInstallmentVo);

            Product product = productMap.get(rentInstallment.getJrProductId());
//            Tenant tenant = tenantMap.get(rentInstallment.getBussinessId());
            RentInstallment installment = rentInstallmentMap.get(rentInstallment.getId());

//            rentInstallmentVo.setCompanyName(product.getCompanyName());
//            rentInstallmentVo.setLoanMessage(tenant.getMerchantName());
            rentInstallmentVo.setApplyOrderNo(installment.getOutOrderNo());
            rentInstallmentVo.setCreateTime(product.getCreateTime());
            rentInstallmentVo.setCompanyName(product.getCompanyName());
            rentInstallmentVo.setAuditMemo(installment.getAuditMemo());
            rentInstallmentVo.setJrProductType(product.getJrProductType());
            rentInstallmentVo.setApplyState(installment.getApplyState());
        }

        return rentInstallmentVoArrayList;


    }

    @Override
    public List<RentInstallmentVo> searchRentInstallmentPage(RentInstallmentVo rentInstallmentVo) {
        return rentInstallmentMapper.searchRentInstallmentPage(rentInstallmentVo);
    }

    @Override
    public RentInstallment selectRentInstallment(Long valueOf) {
        return rentInstallmentMapper.selectRentInstallment(valueOf);
    }

    @Override
    public int updateRentInstallment(RentInstallment rentInstallment) {
        return rentInstallmentMapper.updateRentInstallment(rentInstallment);
    }
}
